package com.xyht.sca_s.student_manage_system.modules.leave.controller;


import com.xyht.sca_s.student_manage_system.auth.util.JwtTokenUtil;
import com.xyht.sca_s.student_manage_system.common.commonReturn.ResponseResult;
import com.xyht.sca_s.student_manage_system.common.desEncodeAndDecode.annotation.SecretBody;
import com.xyht.sca_s.student_manage_system.modules.leave.entity.SmsLeave;
import com.xyht.sca_s.student_manage_system.modules.leave.entity.req.SmsLeaveAddReq;
import com.xyht.sca_s.student_manage_system.modules.leave.entity.req.SmsLeaveAllowReq;
import com.xyht.sca_s.student_manage_system.modules.leave.entity.req.SmsLeaveConfirmResp;
import com.xyht.sca_s.student_manage_system.modules.leave.service.SmsLeaveService;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 请假管理 前端控制器
 * </p>
 *
 * @author XYHT
 * @since 2022-12-06
 */
@RestController
@RequestMapping("/leave")
@SecretBody
public class SmsLeaveController {
    @Autowired
    private SmsLeaveService smsLeaveService;
    @Autowired
    private JwtTokenUtil jwtTokenUtil;

    @ApiModelProperty(value = "请假申请")
    @PostMapping("/student/add")
    public ResponseResult addLeaveProcess(@RequestBody SmsLeaveAddReq smsLeaveAddReq, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsLeaveService.addLeave(smsLeaveAddReq, user_id);
    }

    @ApiModelProperty(value = "取消请假申请")
    @PostMapping("/student/cancel")
    public ResponseResult applyLeaveCancel(@RequestBody SmsLeave smsLeave, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsLeaveService.applyLeaveCancel(smsLeave, user_id);
    }

    @ApiModelProperty(value = "学生获取申请列表")
    @GetMapping("/student/get")
    public ResponseResult studentGetLeaveList(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                              @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                              HttpServletRequest request
    ) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsLeaveService.studentGetLeaveList(pageSize, pageNum, user_id);
    }

    @ApiModelProperty(value = "学生获取申请历史记录")
    @GetMapping("/student/history")
    public ResponseResult studentHistoryGetLeaveList(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                     @RequestParam(required = false) Integer allow_flag,
                                                     @RequestParam(required = false) Integer leaveType,
                                                     @RequestParam(required = false) String bgDate,
                                                     @RequestParam(required = false) String endDate,
                                                     HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsLeaveService.studentHistoryGetLeaveList(pageSize, pageNum, allow_flag, user_id,leaveType,bgDate,endDate);
    }

    @ApiModelProperty(value = "请假审批")
    @PostMapping("/teacher/allow")
    public ResponseResult leaveAllow(@RequestBody SmsLeaveAllowReq smsLeaveAllowReq, HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsLeaveService.leaveAllow(smsLeaveAllowReq, user_id);
    }

    @ApiModelProperty(value = "教师获取审批列表")
    @GetMapping("/teacher/get")
    public ResponseResult teacherAllowGetLeaveList(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                   @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                   @RequestParam(required = false) Integer leaveType,
                                                   @RequestParam(required = false) String applyName,
                                                   @RequestParam(required = false) String bgDate,
                                                   @RequestParam(required = false) String endDate,
                                                   HttpServletRequest request
    ) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsLeaveService.teacherAllowGetLeaveList(pageSize, pageNum, user_id,leaveType,applyName,bgDate,endDate);
    }

    @ApiModelProperty(value = "教师获取历史审批列表")
    @GetMapping("/teacher/history")
    public ResponseResult teacherHistoryGetLeaveList(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                                     @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                     @RequestParam(required = false) Integer allow_flag,
                                                     @RequestParam(required = false) Integer leaveType,
                                                     @RequestParam(required = false) String applyName,
                                                     @RequestParam(required = false) String bgDate,
                                                     @RequestParam(required = false) String endDate,
                                                     HttpServletRequest request
    ) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsLeaveService.teacherHistoryGetLeaveList(pageSize, pageNum,allow_flag,user_id,leaveType,applyName,bgDate,endDate);
    }

    @ApiModelProperty(value = "获取申请详情")
    @GetMapping("/detail")
    public ResponseResult studentGetLeaveList(@RequestParam("leave_id") String leave_id) {
        return smsLeaveService.getLeaveDetail(leave_id);
    }

    @ApiModelProperty(value = "离校返校确认")
    @PostMapping("/confirm")
    public ResponseResult leaveConfirm(@RequestBody SmsLeaveConfirmResp smsLeaveConfirmResp,HttpServletRequest request) {
        String user_id = jwtTokenUtil.getUserIdByRequest(request);
        return smsLeaveService.leaveConfirm(smsLeaveConfirmResp,user_id);
    }



}

